
//
//  YLUserInfo.m
//  BaseProject
//
//  Created by YLCai on 16/11/30.
//  Copyright © 2016年 YLCai. All rights reserved.
//

#import "YLUserInfo.h"

static YLUserInfo *userInfo = nil;

@implementation YLUserInfo

/*  通过初始化userIfo并保存在本地(单利模式)   */
+(instancetype)getuserInfoWithDic:(NSDictionary *)dic{
    userInfo = [[YLUserInfo alloc] initWithDictionary:dic];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userInfo];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:USERINFO];
    [[NSUserDefaults standardUserDefaults] synchronize];//及时存储数据
    return userInfo;
}

-(id)initWithDictionary:(NSDictionary *)dic{
    if (self = [super init]) {
        [self mj_setKeyValues:dic];
    }
    return self;
}
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"yldescription" : @"description"};
}
/*  获取用户已登陆的信息 */
+(instancetype)shareUserInfo{
    
    if (userInfo == nil) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:USERINFO];
        if (data) {
            userInfo =[NSKeyedUnarchiver unarchiveObjectWithData:data];
            return userInfo;
        }
    }
    return userInfo;
}

/*  判断用户是否登陆 */
+(BOOL)isLogIn{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:USERINFO];
    if (data) {
        userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        if (![NSString stringIsNull:userInfo.token]) {
            return YES;
        }else{
            return NO;
        }
    }else{
        return NO;
    }
}

/*  退出登陆 */
+(instancetype)logout{
    userInfo = nil;
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERINFO];
    return userInfo;
}

/*  保存当前userInfo */
+(void)saveUser:(YLUserInfo *)userInfo{
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:userInfo];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:USERINFO];
    [[NSUserDefaults standardUserDefaults] synchronize];//及时存储数据
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.userId forKey:@"userId"];
    [aCoder encodeObject:self.avatar forKey:@"avatar"];
    [aCoder encodeObject:self.fansNum forKey:@"fansNum"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
    [aCoder encodeObject:self.nickname forKey:@"nickname"];
    [aCoder encodeObject:self.token forKey:@"token"];
    [aCoder encodeObject:self.yldescription forKey:@"yldescription"];
    [aCoder encodeObject:self.attentionNum forKey:@"attentionNum"];
    [aCoder encodeObject:self.provinceName forKey:@"provinceName"];

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.birthday forKey:@"birthday"];
    [aCoder encodeObject:self.gender forKey:@"gender"];
    [aCoder encodeObject:self.province forKey:@"province"];
    [aCoder encodeObject:self.city forKey:@"city"];
    [aCoder encodeObject:self.cityName forKey:@"cityName"];
    [aCoder encodeObject:self.district forKey:@"district"];
    [aCoder encodeObject:self.address forKey:@"address"];
    [aCoder encodeObject:self.industryId forKey:@"industryId"];
    [aCoder encodeObject:self.investmentBudget forKey:@"investmentBudget"];
    [aCoder encodeObject:self.operationMode forKey:@"operationMode"];
    [aCoder encodeObject:self.itemid forKey:@"itemid"];
    [aCoder encodeObject:self.createdAt forKey:@"createdAt"];
    [aCoder encodeObject:self.updateAt forKey:@"updateAt"];
    [aCoder encodeObject:self.stage forKey:@"stage"];
    [aCoder encodeObject:self.stageName forKey:@"stageName"];
    [aCoder encodeObject:self.background forKey:@"background"];
    [aCoder encodeObject:self.industry forKey:@"industry"];

}

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.userId = [aDecoder decodeObjectForKey:@"userId"];
        self.avatar = [aDecoder decodeObjectForKey:@"avatar"];
        self.fansNum = [aDecoder decodeObjectForKey:@"fansNum"];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
        self.nickname = [aDecoder decodeObjectForKey:@"nickname"];
        self.token = [aDecoder decodeObjectForKey:@"token"];
        self.yldescription = [aDecoder decodeObjectForKey:@"yldescription"];
        self.attentionNum = [aDecoder decodeObjectForKey:@"attentionNum"];
        self.provinceName = [aDecoder decodeObjectForKey:@"provinceName"];

        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.birthday = [aDecoder decodeObjectForKey:@"birthday"];
        self.gender = [aDecoder decodeObjectForKey:@"gender"];
        self.province = [aDecoder decodeObjectForKey:@"province"];
        self.city = [aDecoder decodeObjectForKey:@"city"];
        self.cityName = [aDecoder decodeObjectForKey:@"cityName"];
        self.district = [aDecoder decodeObjectForKey:@"district"];
        self.address = [aDecoder decodeObjectForKey:@"address"];
        self.industryId = [aDecoder decodeObjectForKey:@"industryId"];
        self.investmentBudget = [aDecoder decodeObjectForKey:@"investmentBudget"];
        self.operationMode = [aDecoder decodeObjectForKey:@"operationMode"];
        self.itemid = [aDecoder decodeObjectForKey:@"itemid"];
        self.createdAt = [aDecoder decodeObjectForKey:@"createdAt"];
        self.updateAt = [aDecoder decodeObjectForKey:@"updateAt"];
        self.stage = [aDecoder decodeObjectForKey:@"stage"];
        self.stageName = [aDecoder decodeObjectForKey:@"stageName"];
        self.background = [aDecoder decodeObjectForKey:@"background"];
        self.industry = [aDecoder decodeObjectForKey:@"industry"];

    }
    return self;
}

@end
